package io.shqyang.quick.slice;

import io.shqyang.quick.ResourceTable;
import io.shqyang.quicklib.base.QuickAbilitySlice;
import ohos.agp.components.surfaceprovider.SurfaceProvider;
import ohos.agp.graphics.Surface;
import ohos.agp.graphics.SurfaceOps;
import ohos.media.common.AVDescription;
import ohos.media.common.Source;
import ohos.media.common.sessioncore.AVElement;
import ohos.media.player.Player;
import ohos.utils.net.Uri;

public class VideoPlayerSlice extends QuickAbilitySlice {

    private SurfaceProvider surfaceProvider;
    private Player player;
    private String WEB_VIDEO_PATH = "https://ss0.bdstatic.com/-0U0bnSm1A5BphGlnYG/cae-legoup-video-target/93be3d88-9fc2-4fbd-bd14-833bca731ca7.mp4";
    private Surface surface;

    @Override
    protected int layoutId() {
        return ResourceTable.Layout_video_slice;
    }

    @Override
    protected void initComponent() {
        surfaceProvider = new SurfaceProvider(this);
        surfaceProvider.getSurfaceOps()
                .get()
                .addCallback(new SurfaceOps.Callback() {
                    @Override
                    public void surfaceCreated(SurfaceOps surfaceOps) {
                        surface = surfaceProvider.getSurfaceOps().get().getSurface();
                    }

                    @Override
                    public void surfaceChanged(SurfaceOps surfaceOps, int i, int i1, int i2) {

                    }

                    @Override
                    public void surfaceDestroyed(SurfaceOps surfaceOps) {

                    }
                });
        surfaceProvider.pinToZTop(true);
        findContainer(ResourceTable.Id_container).addComponent(surfaceProvider);
        player = new Player(this);
        AVDescription bean =
                new AVDescription.Builder()
                        .setTitle("web_video_01")
                        .setIMediaUri(Uri.parse(WEB_VIDEO_PATH))
                        .setMediaId(WEB_VIDEO_PATH)
                        .build();
        String mediaUriStr = new AVElement(bean, AVElement.AVELEMENT_FLAG_PLAYABLE).getAVDescription().getMediaUri().toString();
        if (mediaUriStr.equals(WEB_VIDEO_PATH)) {
            toast("一模一样");
        }
        findComponent(ResourceTable.Id_play_btn).setClickedListener(v->{
            player.setSource(new Source(mediaUriStr));
            player.setVideoSurface(surface);
            player.prepare();
            player.play();
            toast("开始播放");
        });
    }

    @Override
    protected void initEvent() {

    }

    @Override
    protected void loadData(boolean isRefresh) {

    }
}
